package com.schoolhelper.post.mapper;

import com.schoolhelper.post.domain.Post;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
public interface PostMapper {
    ArrayList<Post> getAllPosts();
    ArrayList<Post> getAllPostsWithPagination(@Param("size") int size, @Param("offset") int offset);
    ArrayList<Post> getPostsByUserId(@Param("userId") Long userId);
    ArrayList<Post> getPostsByUserIdWithPagination(@Param("userId") Long userId, @Param("size") int size, @Param("offset") int offset);
    int insertPost(Post post);
    int insertPostImage(@Param("postId") Long postId, @Param("imageUrl") String imageUrl, @Param("createTime") java.util.Date createTime);
    
    // 点赞相关方法
    @Insert("INSERT INTO post_like (post_id, user_id, create_time) VALUES (#{postId}, #{userId}, NOW())")
    int insertPostLike(@Param("postId") Long postId, @Param("userId") Long userId);
    
    @Delete("DELETE FROM post_like WHERE post_id = #{postId} AND user_id = #{userId}")
    int deletePostLike(@Param("postId") Long postId, @Param("userId") Long userId);
    
    @Select("SELECT COUNT(*) FROM post_like WHERE post_id = #{postId} AND user_id = #{userId}")
    int isPostLikedByUser(@Param("postId") Long postId, @Param("userId") Long userId);
    
    @Update("UPDATE post SET like_count = like_count + 1 WHERE id = #{postId}")
    int incrementLikeCount(@Param("postId") Long postId);
    
    @Update("UPDATE post SET like_count = like_count - 1 WHERE id = #{postId} AND like_count > 0")
    int decrementLikeCount(@Param("postId") Long postId);
}
